

<script type="text/javascript" charset="utf-8" src="../ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="../ueditor/ueditor.all.min.js"> </script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" charset="utf-8" src="../ueditor/lang/zh-cn/zh-cn.js"></script>
<style>
    .title {
        margin-top: 50px;
    }
    .editor-container {
        background-color: #FFFFFF;
        border-radius: 20px;
        padding: 20px;
        max-width: 1064px;
    }
</style>
<div>
    <p class="title"></p>
    <div class="editor-container">
        <form role="form" class="form" action="">
            <div class="row">
                <div class="form-group col-sm-6">
                    <label for="name">名称</label>
                    <input type="text" class="form-control" id="name" placeholder="请输入名称">
                </div>
                <div class="form-group col-sm-2">
                    <label for="classify">分类</label>
                    <select class="form-control" id="classify" placeholder="请选择优先级">
                        <% classifies.forEach(function (item) { %>
                        <option value="<%= item._id %>"><%=item.name%></option>
                        <% }) %>
                    </select>
                </div>
                <div class="form-group col-sm-2">
                    <label for="weight">优先级</label>
                    <select class="form-control" id="weight" placeholder="请选择优先级">
                        <option value="1">置顶</option>
                        <option value="2">极高</option>
                        <option value="3">高</option>
                        <option value="4">默认</option>
                        <option value="5">低</option>
                        <option value="6">极低</option>
                    </select>
                </div>

                <div class="form-group col-sm-2">
                    <label id="data-id" for="">　</label>
                    <button id="submit" type="button" class="btn btn-success btn-block">提交</button>
                </div>
            </div>
        </form>
        <div class="form-group">
            <label for="name">内容</label>
            <div style="text-align: center">
                <div id="editor" type="text/plain" style="width:1024px;height:500px;"></div>
            </div>
        </div>

    </div>

</div>
<script type="text/javascript">

//实例化编辑器
//建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
var ue = UE.getEditor('editor');
$(function () {
    docId = null;
    <% if (docId) { %>
    docId = "<%=docId %>";
    $.get('/ueditor/detail',{id:docId},function (data) {
        if (data.code === 0) {
            let doc = data.data;
            $('#name').val(doc.name);
            $("#weight option").each(function(i,n){
                if($(n).val()=== String(doc.weight))
                {
                    $(n).attr("selected",true);
                } else  {
                    $(n).attr("selected",false);
                }
            });
            $("#classify option").each(function(i,n){
                if($(n).val()=== doc.classify._id)
                {
                    $(n).attr("selected",true);
                } else  {
                    $(n).attr("selected",false);
                }
            })
            ue.addListener("ready", function () {
                ue.setContent(doc.content || '请输入内容');
            });
        } else {
            showError('未知错误');
            docId = null;
        }
    });
    <% } %>
});

$('#submit').click(function () {
    let id = docId;
    let content = ue.getContent();
    let weight = $('#weight').val();
    let name = $('#name').val();
    let classify = $('#classify').val();
    let param = {
        id : id,
        content : content,
        weight : weight,
        name : name,
        classify : classify
    }
    let url = id ? '/ueditor/edit' : '/ueditor/add';
    $.post(url,param,function (data) {
        if (data.code === 0) {
            showSuccess((id ?'已更新' : '已添加') + '文档 ' + name);
            setTimeout(function () {
                window.location.href = './list';
            },1000);
        } else {
            showError(data.message);
        }
    })
});

</script>
